package top.milkbox.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import top.milkbox.service.EmailVerificationCodeService;
import top.milkbox.service.PictureVerificationCodeService;
import top.milkbox.utils.GuoUtilsRandom;

import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpSession;

@Service
@Slf4j
public class EmailVerificationCodeServiceImpl implements EmailVerificationCodeService {

    // 注入邮件操作类
    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private PictureVerificationCodeService pvcService;

    @Value("${spring.mail.username}")
    private String 发送者的邮箱;

    @Value("${info.email-pass}")
    private boolean emailPass; // 是否允许发送邮件

    @Override
    public Boolean sendEmail(HttpSession session, String code, String targetEmail) {
        // 先校验图片验证码
        if (!pvcService.check(session, code)) {
            log.debug("图片验证码校验未通过，不进行邮件发送");
            return false;
        }

        // 校验通过

        // 将邮箱地址写入session
        session.setAttribute("userEmail", targetEmail);
        // 生成随机数
        String emailVerificationCode = GuoUtilsRandom.getRandomString(6, "0123456789");
        // 将邮箱验证码存入session
        session.setAttribute("emailVerificationCode", emailVerificationCode);
        log.debug("正在向(" + targetEmail + ")发送验证码(" + emailVerificationCode + ")...");

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 创建MimeMessageHelper对参数进行设置
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 发送者邮箱(昵称)，后面如果有小括号，则会在显示的时候替换邮箱
            helper.setFrom(发送者的邮箱 + "(奶盒平台)");
            // 收件人邮箱
            helper.setTo(targetEmail);
            // 标题
            helper.setSubject("您的奶盒账号邮箱验证码");

            String context = "" +
                    "<div style=\"text-align: center;\">\n" +
                    "\t<h1>您的奶盒账号邮箱验证码</h1>\n" +
                    "\t<div style='color:red; font-size:28px; border: #1b6d85 2px solid;'>" + emailVerificationCode + "</div>\n" +
                    "\t<div>此验证码有效期为30分钟，收到验证码后请尽快完成注册</div>\n" +
                    "\t<div>非常感谢您的支持，这封邮件是用开发者本人的QQ邮箱发送的，请不要拉黑！</div>\n" +
                    "</div>";

            // 正文，指定正文内容，后面参数设置为true表示会将正文内容解析为html，设置为false则不会解析，默认为false
            helper.setText(context, true);

            // 设置完毕
            if (emailPass) {
                javaMailSender.send(mimeMessage);
                log.debug("邮件发送成功");
            } else {
                log.warn("手动禁用邮件的发送，邮箱验证码为：" + emailVerificationCode);
            }
            return true;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Boolean checkEmail(HttpSession session, String userEmail, String emailCode) {
        if (userEmail.equals(session.getAttribute("userEmail"))
                && emailCode.equals(session.getAttribute("emailVerificationCode"))) {
            log.debug("邮箱验证码校验成功");

            //从session中删除邮箱验证码
            session.removeAttribute("emailVerificationCode");
            return true;
        }
        log.debug("邮箱验证码校验失败");
        return false;
    }


}
